package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

@Api(tags = "数据统计接口")
@RestController
@RequestMapping("/admin/report")
@Slf4j
public class ReportController {
    @Autowired
    private ReportService reportService;

    /**
     * 营业额统计
     *
     * @param begin
     * @param end
     * @return
     */
    @ApiOperation("统计营业额数据")
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> turnoverStatistics(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate begin, //日期格式为YYYY-MM-DD
                                                       @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end) {
        log.info("查询营业额数据：{}到{}", begin, end);
        TurnoverReportVO vo = reportService.turnoverStatistics(begin, end);
        return Result.success(vo);
    }

    /**
     * 用户统计
     *
     * @param begin
     * @param end
     * @return
     */
    @ApiOperation("统计用户数据")
    @GetMapping("/userStatistics")
    public Result<UserReportVO> userStatistics(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate begin, //日期格式为YYYY-MM-DD
                                               @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end) {
        log.info("查询用户数据：{}到{}", begin, end);
        UserReportVO vo = reportService.userStatistics(begin, end);
        return Result.success(vo);
    }

    /**
     * 订单统计
     *
     * @param begin
     * @param end
     * @return
     */
    @ApiOperation("统计订单数据")
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> ordersStatistics(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate begin, //日期格式为YYYY-MM-DD
                                                  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end) {
        log.info("查询订单数据：{}到{}", begin, end);
        OrderReportVO vo = reportService.ordersStatistics(begin, end);
        return Result.success(vo);
    }

    /**
     * 销量排名
     *
     * @param begin
     * @param end
     * @return
     */
    @ApiOperation("查询销量排名top10")
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> top10(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate begin, //日期格式为YYYY-MM-DD
                                            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end) {
        log.info("查询销量排名top10：{}到{}", begin, end);
        SalesTop10ReportVO vo = reportService.top10(begin, end);
        return Result.success(vo);
    }


    /**
     * 导出Excel数据
     *
     * @param response
     */

    @ApiOperation("导出Excel报表")
    @GetMapping("/export")
    public Result export(HttpServletResponse response) {
        log.info("导出Excel报表");
        reportService.exportBusinessData(response);
        return Result.success();
    }
}
